﻿using CommonBase.ResponseBase;
using CustomControls.PoptipCtr.Base;
using LogProjectManager;
using ManagementToolClient.BaseViewModels;
using ManagementToolClient.Models;
using Newtonsoft.Json;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ManagementToolClient.ViewModels
{
    internal class AddDevicePageViewModel : MiddleDalogViemModelBase, ILayDialogWindowAware
    {
        public new event Action<ILayDialogResult> RequestClose;

        #region Property
        private DeviceManagerModel _DeviceManagerItem;

        public DeviceManagerModel DeviceManagerItem
        {
            get { return _DeviceManagerItem; }
            set
            {
                SetProperty(ref _DeviceManagerItem, value);

            }
        }
        #endregion


        #region cmd
        private DelegateCommand _CloseCommand;
        public DelegateCommand CloseCommand =>
            _CloseCommand ?? (_CloseCommand = new DelegateCommand(ExcuteCloseCommand));



        private DelegateCommand _OKxCommand;
        public DelegateCommand OKxCommand =>
            _OKxCommand ?? (_OKxCommand = new DelegateCommand(ExcuteOKxCommand));




        #endregion



        public new bool CanCloseDialog()
        {
            return true;
        }

        public new void OnDialogClosed()
        {

        }



        public void OnDialogOpened(ILayDialogParameter parameters)
        {
            DeviceManagerItem = new DeviceManagerModel();
            DeviceManagerItem.companyid = parameters.GetValue<string>("UUID");
        }

        private void ExcuteCloseCommand()
        {
            BackPageCommandMethod(ButtonResult.No);
        }

        private void ExcuteOKxCommand()
        {
            Task.Run(async () => {
                try
                {
                    var url = "http://127.0.0.1:8081" + "/api/ToAddDeviceByCompany?" + $"_companyid={DeviceManagerItem.companyid}" + "&" + $"_robatmac={DeviceManagerItem.robatmac}" + "&" + $"_license={DeviceManagerItem.license}" + "&" + $"_effecttime={DeviceManagerItem.effecttime}";


                    var httpClient = new System.Net.Http.HttpClient();
                    HttpContent content = new StringContent("", Encoding.UTF8, "application/json");

                    var response = httpClient.PostAsync(url, content).Result;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string responseBody = await response.Content.ReadAsStringAsync();
                        var _itemResult = JsonConvert.DeserializeObject<AuthorizationCodeResponseBody>(responseBody);
                        if (_itemResult.code == ((int)ErrorCode.OK).ToString())
                        {
                            BackPageCommandMethod(ButtonResult.Yes);
                        }
                        else
                        {
                            LogManager.WriteLog(LogTypeEnum.Normal, "ExcuteOKxCommand", "新增失败");
                        }

                    }
                    else
                    {
                        LogManager.WriteLog(LogTypeEnum.Exception, "ExcuteOKxCommand", "接口访问失败");
                    }
                }
                catch (Exception ex)
                {
                    LogManager.WriteLog(LogTypeEnum.Exception, "GetDate", ex.Message);
                }
            });
        }


        private void BackPageCommandMethod(ButtonResult itemResult)
        {
            LayDialogResult dialogResult = new LayDialogResult();
            dialogResult.Result = itemResult;
            RequestClose?.Invoke(dialogResult);
        }
    }
}
